package czy.demo.config;


import org.apache.catalina.connector.Connector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置服务器，要继承最终的TomcatReactiveWebServerFactory
 * 可以覆盖它的其中一个方法
 */
@Configuration
public class SpringServerConfig{

    private static final Logger logger = LoggerFactory.getLogger(SpringServerConfig.class);

    /*  添加服务器定制化器，用于定制化内嵌的Tomcat实例，增加配置 */
    @Bean
    public WebServerFactoryCustomizer webServerFactoryCustomizer(){

        return new WebServerFactoryCustomizer() {
            @Override
            public void customize(WebServerFactory factory) {
                if(factory instanceof TomcatServletWebServerFactory){
                    /* 添加一个底层的连接器，监听8080端口 */
                    Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
                    connector.setScheme("http");
                    connector.setPort(8080);
                    connector.setMaxPostSize(100000000);
                    connector.setAllowTrace(true);
                    connector.setMaxCookieCount(100);
                    connector.setMaxParameterCount(100);
                    connector.setMaxSavePostSize(100000000);
                    connector.setSecure(false);
                    connector.setURIEncoding("utf-8");
                    connector.setUseBodyEncodingForURI(false);
                    ((TomcatServletWebServerFactory) factory).addAdditionalTomcatConnectors(connector);
                    ((TomcatServletWebServerFactory) factory).getSession().getCookie().setSecure(false);
                }
            }
        };
    }
}
